Main ----- Copyright Up Previous Next

Conditionals

General syntax

Conditionals looks like that:
<$if COND=expression>

  code to be processed if condition matches

<$elseif COND=expression>

  (optional) code to be processed if alternative condition matches

<$else>

  (optional) code to be processed if none of previous conditions matched

</$if>

Both <$if> and <$elseif> require a boolean attribute COND; false is represented by an empty string, true by any non-empty string. Normally, you will like to set COND using expressions.

Some simple examples

Now let's see how this works in practice:
    <$if COND=(NAME="sepp")>
       You must be sepp!
    </$if>

This one inserts the text "You must be sepp!", if the attribute NAME has the value "sepp". Note that the "="-operator performs a case-insensitive string-comparison, so setting NAME="SEPP" would lead to the same result.

Now let's extend this:
    <$if COND=(NAME="sepp")>
       You must be sepp!
    <$else>
       I don't know you.
    </$if>

Setting NAME="sepp" will create the same text as above. Any other value for NAME will insert "I don't know you.".

Nesting conditionals

Nesting them is also possible, of course:

    <$if COND=(NAME="sepp")>
       You must be sepp!
       <$if COND=(HOME="austria")>
           Hollareiduliö! An Austrian!
       <$else>
           <(HOME)> is a strange country.
       </$if>
    <$else>
       A strange guy from a strange place.
    </$if>

Conditionals and macros

You can not compare hsc's <$if> to primitive and clumsy #if of the C-preprocessor. The main difference is that you can use <$if> inside macros and that expressions are recalculated for every new call of the macro.

    <$macro TRY-A HREF:uri/r TITLE:string/r>
    <$if COND=(Exists(HREF))>
        <A HREF=(HREF)><(TITLE)></A> <* insert link to HREF *>
    <$else>
        <(TITLE)>                    <* insert plain title *>
    </$if>
    </$macro>

This one inserts a link to an URI specified with HREF, using TITLE as link text; but only, if the destination (local) URI can be accesed. If the required document is not available, only the plain text without a link will be inserted.

The "/r" behind the declaration of the macro-attributes is short for "/required" and means that the macro needs both of these attributes to be set during the macro-call.

For example, you can utilize this macro using

    You should read the document about recent
    <TRY-A HREF="../bugfixes.html" TITLE="bufixes">
    if there are any.

This leads to the text

    You should read the document about recent bugfixes if there are any.

with a anchor around the term "bugfixes" if the document "../bugfixes.html" exists.

Limitations

However, conditionals always must be closed inside a macro. For instace, one could try to create multiple versions of one document using the same source to support multiple languages. For this case, the following macros seems to be handy:

    <$macro E><$if COND=(LANGUAGE="english")></$macro>
    <$macro /E></$if></$macro>
But trying to use them like
    language independent stuff
    <E>english people only</E>
will lead to a
    Fatal error 3: unexpected end of file (</$IF> expected)
because hsc only tries to find a closing </$IF> inside the current macro context.